/**
 * 数字格式化
 * 方法ToDigits2和formatMoney是之前后端写的，这里复制过来
 *
 * formaTime:
 * @param time 日期
 * @param [format] 格式 {"yyy-mm-dd"（默认）|"yyyy-mm-dd hh:mm:ss"|"yyyy/mm/dd"|"yyyy/mm/dd hh:mm:ss"}
 *
 *
 */
var UFormatter = (function(){
    return {
        ToDigits2 : function (floatVal) {
            var strResult = parseFloat(floatVal).toFixed(4).toString();
            return parseFloat(strResult.substr(0, strResult.length - 2));
        },
        formatMoney : function (m, fixedNumParm) {
            m = m.toString();
            var newStr = "";
            var fixedNum = fixedNumParm || 2;
            if (m.indexOf('.') > -1 || fixedNumParm != undefined) {
                m = parseFloat(m);
                var a = m.toFixed(fixedNum).split('.')[0];
                var b = m.toFixed(fixedNum).split('.')[1];
                if (a.length > 12) {
                    newStr = a.substr(0, a.length - 12) + "," + a.substr(a.length - 12, 3) + "," + a.substr(a.length - 9, 3) + "," + a.substr(a.length - 6, 3) + "," + a.substr(a.length - 3, 3) + "." + b;
                } else if (a.length > 9) {
                    newStr = a.substr(0, a.length - 9) + "," + a.substr(a.length - 9, 3) + "," + a.substr(a.length - 6, 3) + "," + a.substr(a.length - 3, 3) + "." + b;
                } else if (a.length > 6) {
                    newStr = a.substr(0, a.length - 6) + "," + a.substr(a.length - 6, 3) + "," + a.substr(a.length - 3, 3) + "." + b;
                } else if (a.length > 3) {
                    newStr = a.substr(0, a.length - 3) + "," + a.substr(a.length - 3, 3) + "." + b;
                } else {
                    newStr = a + "." + b;
                }
            } else {
                if (m.length > 12) {
                    newStr = m.substr(0, m.length - 12) + "," + m.substr(m.length - 12, 3) + "," + m.substr(m.length - 9, 3) + "," + m.substr(m.length - 6, 3) + "," + m.substr(m.length - 3, 3);
                } else if (m.length > 9) {
                    newStr = m.substr(0, m.length - 9) + "," + m.substr(m.length - 9, 3) + "," + m.substr(m.length - 6, 3) + "," + m.substr(m.length - 3, 3);
                } else if (m.length > 6) {
                    newStr = m.substr(0, m.length - 6) + "," + m.substr(m.length - 6, 3) + "," + m.substr(m.length - 3, 3);
                } else if (m.length > 3) {
                    newStr = m.substr(0, m.length - 3) + "," + m.substr(m.length - 3, 3);
                } else {
                    newStr = m;
                }
            }
            return newStr;
        },
        formatTime : function(time, format){
            var _time = new Date(time), TArr;
            var format = format ? format.toLowerCase() : "yyyy-mm-dd";
            var Ddigit = function(n){
                var _n = Number(n);
                _n = _n > 9 ? _n : "0"+_n;
                return _n;
            };
            if(typeof time == "string" && time.indexOf("-") > -1){
                var ps = time.split(" ");
                var pd = ps[0].split("-");
                var pt = ps.length>1 ? ps[1].split(":"): ["00","00","00"];
                TArr = [pd[0], Ddigit(pd[1]), Ddigit(pd[2]), Ddigit(pt[0]), Ddigit(pt[1]), Ddigit(pt[2])];
            } else {
                var _y = _time.getFullYear();
                var _mon = _time.getMonth() + 1;
                var _d = _time.getDate();
                var _h = _time.getHours();
                var _m = _time.getMinutes();
                var _s = _time.getSeconds();
                TArr = [_y, Ddigit(_mon), Ddigit(_d), Ddigit(_h), Ddigit(_m), Ddigit(_s)];
            }
            var TStr;
            if(format == "yyyy-mm-dd"){
                TStr = TArr[0] + "-" + TArr[1] + "-" + TArr[2];
            }else if(format == "yyyy-mm-dd hh:mm"){
                TStr = TArr[0] + "-" + TArr[1] + "-" + TArr[2] + " " + TArr[3] + ":" + TArr[4];
            }else if(format == "yyyy-mm-dd hh:mm:ss"){
                TStr = TArr[0] + "-" + TArr[1] + "-" + TArr[2] + " " + TArr[3] + ":" + TArr[4] +":" + TArr[5];
            }else if(format == "yyyy/mm/dd"){
                TStr = TArr[0] + "/" + TArr[1] + "/" + TArr[2];
            }else if(format == "yyyy/mm/dd hh:mm:ss"){
                TStr = TArr[0] + "/" + TArr[1] + "/" + TArr[2] + " " + TArr[3] + ":" + TArr[4] +":" + TArr[5];
            }
            return TStr;
        },
        formatBankCard:function (bankid) {
            var v = bankid;
            if(/\S{5}/.test(v)){
                v = v.replace(/\s/g, '').replace(/(.{4})/g, "$1 ");
            }
            return v;
        }
    };
})();
module.exports = UFormatter;